package com.yjc.drygoods.coupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.yjc.common.to.CouponTo;
import com.yjc.common.vo.MemberRespVo;
import com.yjc.drygoods.coupon.entity.CouponEntity;
import com.yjc.drygoods.coupon.service.CouponHistoryService;
import com.yjc.drygoods.coupon.service.CouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yjc.common.utils.PageUtils;
import com.yjc.common.utils.Query;

import com.yjc.drygoods.coupon.dao.CouponHistoryDao;
import com.yjc.drygoods.coupon.entity.CouponHistoryEntity;


@Service("couponHistoryService")
public class CouponHistoryServiceImpl extends ServiceImpl<CouponHistoryDao, CouponHistoryEntity> implements CouponHistoryService {

    @Autowired
    CouponService couponService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CouponHistoryEntity> page = this.page(
                new Query<CouponHistoryEntity>().getPage(params),
                new QueryWrapper<CouponHistoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CouponEntity> getCouponByMemberId(Long memberId) {

        List<CouponHistoryEntity> couponHistorys = this.list(new QueryWrapper<CouponHistoryEntity>()
                .eq("member_id", memberId));
        if (couponHistorys != null && couponHistorys.size() > 0) {
            List<Long> couponIds = couponHistorys.stream().map(item -> item.getCouponId()).collect(Collectors.toList());
            Map<Long, Integer> map = couponHistorys.stream().collect(Collectors.toMap(CouponHistoryEntity::getCouponId, CouponHistoryEntity::getUseType));
            List<CouponEntity> coupons = couponService.list(new QueryWrapper<CouponEntity>().in("id", couponIds));
            List<CouponEntity> res = coupons.stream().map(item -> {
                item.setUseStatus(map.get(item.getId()));
                return item;
            }).collect(Collectors.toList());
            return res;
        }
        return null;
    }

    @Override
    public void updateMemberCouponUseType(List<CouponHistoryEntity> res) {
        for (CouponHistoryEntity re : res) {
            this.update(re, new UpdateWrapper<CouponHistoryEntity>().eq("coupon_id", re.getCouponId()).eq("use_type", 0));
        }
    }

    @Override
    public void saveOrUpdateCouponHistory(MemberRespVo memberRespVo, CouponTo coupon) throws Exception {
        int count = this.count(new QueryWrapper<CouponHistoryEntity>()
                .eq("member_id", memberRespVo.getId())
                .eq("coupon_id", coupon.getId()));
        if (count < coupon.getPerLimit()) {
            CouponHistoryEntity couponHistory = new CouponHistoryEntity();
            couponHistory.setCouponId(coupon.getId());
            couponHistory.setMemberId(memberRespVo.getId());
            couponHistory.setUsername(memberRespVo.getUsername());
            couponHistory.setGetType(1);
            couponHistory.setCreateTime(new Date());
            couponHistory.setUseType(0);
            this.save(couponHistory);
        } else {
            throw new Exception("该优惠券领取数量超过最大值");
        }

    }

}